<?php
// menggunakan class phpExcelReader
require_once("reader.php");

// koneksi ke mysql
mysql_connect("localhost", "root", "debi");
mysql_select_db("data_excel");

if((!empty($_FILES['userfile'])) && ($_FILES['userfile']['error'] == 0)) {
	//Check if the file is JPEG image and it's size is less than 350Kb
	$filename = basename($_FILES['userfile']['name']);
	$ext = substr($filename, strrpos($filename, '.') + 1);
	if (($_FILES['userfile']["size"] > 350000)) {
		//Determine the path to which we want to save this file
		$newname = dirname(__FILE__).'/upload/'.$filename;
		//Check if the file with the same name is already exists on the server
		if (!file_exists($newname)) {
			//Attempt to move the uploaded file to it's new place
			if ((move_uploaded_file($_FILES['userfile']['tmp_name'],$newname))) {
				echo "It's done! The file has been saved as: ".$newname;
			} else {
				echo "Error: A problem occurred during file upload!";
			}
		} else {
			echo "Error: File ".$_FILES['userfile']["name"]." already exists";
		}
	} else {
		echo "Error: Only .jpg images under 350Kb are accepted for upload";
	}
} else {
	echo "Error: No file uploaded";
}

// membaca file excel yang diupload
$data = new Spreadsheet_Excel_Reader();

// membaca jumlah baris dari data excel
chmod(dirname(__FILE__).'/upload/'.$filename, 0777); 
$data->read("$filename");
$baris = $data->sheets[0]['numRows'];
// nilai awal counter untuk jumlah data yang sukses dan yang gagal diimport
$sukses = 0;
$gagal = 0;

// import data excel mulai baris ke-2 (karena baris pertama adalah nama kolom)
for ($i=2; $i <= $baris; $i++)
{
	for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
		echo "\"".$data->sheets[0]['cells'][$i][$j]."\",";
	}
	echo "\n";

	/*$sp = $data->val($i, 1);
	 $hp = $data->val($i, 2);
	 $telpon = $data->val($i, 3);
	 $pin = $data->val($i, 4);
	 $balance = $data->val($i, 5);
	 $lastbalance = $data->val($i, 6);
	 $nama = $data->val($i, 7);
	 $nama_pemilik = $data->val($i, 8);
	 $alamat = $data->val($i, 9);
	 $ket = $data->val($i, 10);
	 $agenid = $data->val($i, 11);
	 $kelompok = $data->val($i, 12);
	 $marketing = $data->val($i, 13);
	 $status = $data->val($i, 14);
	 $send = $data->val($i, 15);
	 $upline = $data->val($i, 16);
	 $aktif = $data->val($i, 17);
	 $bonus = $data->val($i, 18);
	 $transfer = $data->val($i, 19);
	 $cluster = $data->val($i, 20);
	 $ip = $data->val($i, 21);
	 $level = $data->val($i, 22);
	 $pengurus = $data->val($i, 23);
	 $limitsaldo = $data->val($i, 24);
	 $statuslimit = $data->val($i, 25);
	 $idkoperasi = $data->val($i, 26);
	 $yahooid = $data->val($i, 27);

	 // setelah data dibaca, sisipkan ke dalam tabel mhs
	 $query = "INSERT INTO stockiest VALUES (
		'$sp',
		'$hp',
		'$telpon',
		'$pin',
		'$balance',
		'$lastbalance',
		'$nama',
		'$nama_pemilik',
		'$alamat',
		'$ket',
		'$agenid',
		'$kelompok',
		'$marketing',
		'$status',
		'$send',
		'$upline',
		'$aktif',
		'$bonus',
		'$transfer',
		'$cluster',
		'$ip',
		'$level',
		'$pengurus',
		'$limitsaldo',
		'$statuslimit',
		'$idkoperasi',
		'$yahooid')";
		$hasil = mysql_query($query);

		// jika proses insert data sukses, maka counter $sukses bertambah
		// jika gagal, maka counter $gagal yang bertambah
		if ($hasil) $sukses++;
		else $gagal++;*/
}

// tampilan status sukses dan gagal
echo "<h3>Proses import data selesai.</h3>";
echo "<p>Jumlah data yang sukses diimport : ".$sukses."<br>";
echo "Jumlah data yang gagal diimport : ".$gagal."</p>";

?>
